package com.zelin.model3;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 用户登录线程
 * 用于实现多用户的访问
 */
public class LoginThread extends Thread{
    //构造Socket对象便于线程传参
    private Socket socket;
    //构造方法
    public LoginThread() {}
    public LoginThread(Socket socket) {
        this.socket=socket;
    }

    InputStream inputStream = null;
    OutputStream outputStream = null;
    ObjectInputStream ois=null;

    public void run(){
        try {
            //获得客户端发送的字节流,返回字节流对象
            inputStream = socket.getInputStream();
            ois=new ObjectInputStream(inputStream);
            //反序列化对象
            User user= (User) ois.readObject();

            //判断用户名密码是否正确
            //正确就返回消息给客户端,提示用户密码正确
            outputStream = socket.getOutputStream();
            String info="";
            if (user.getName().equals("admin")&&user.getPassword().equals("123")){
                info="密码输入正确!!!";
                outputStream.write(info.getBytes());
            }else {
                info="用户名或密码输入错误!";
                outputStream.write(info.getBytes());
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.flush();
                outputStream.close();
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
